//
//  HZOrderConfirmAddressHeaderView.m
//  app
//
//  Created by 刘华舟 on 15/5/6.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZOrderConfirmAddressHeaderView.h"

#import "MyToast.h"

//view
#import "HZTableView.h"

//model
#import "HZOrderByerAddressModel.h"

static const CGFloat kOrderCellLabelHeight = 24;
static const CGFloat kOrderCellKeyLabelWidth = 68;

@interface HZOrderConfirmAddressHeaderView()<UITextFieldDelegate>

@property (strong, nonatomic) UILabel* nameKeyLabel;
@property (strong, nonatomic) UILabel* addressKeyLabel;



@property (strong, nonatomic) UILabel* groupLabel;
@property (strong, nonatomic) UILabel *buildingLabel;
@property (strong, nonatomic) UILabel *roomLabel;

@property (strong, nonatomic) UILabel* phoneKeyLabel;



@property (strong, nonatomic) HZOrderByerAddressModel* model;

@end

@implementation HZOrderConfirmAddressHeaderView

- (void)drawRect:(CGRect)rect{
    [self drawBackground:self.bounds color:HZ_WHITE_COLOR];
}


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.frame = CGRectOf([UBO_SCREEN fx:UB_LEFT fy:UB_TOP], [UBO_SCREEN fw:UB_WIDTH oh:HZOrderConfirmAddressHeaderViewHeight]);
        self.contentMode = UIViewContentModeRedraw;

        
        //收货人 key
        self.nameKeyLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP oy:kSizeMallViewMargin/4], [self.ubo ow:kOrderCellKeyLabelWidth oh:kOrderCellLabelHeight])];
        self.nameKeyLabel.textAlignment = NSTextAlignmentLeft;
        self.nameKeyLabel.textColor = HZ_MALL_TEXT_GRAY_COLOR;
        self.nameKeyLabel.font = kHZFont_Content_S;
        self.nameKeyLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.nameKeyLabel.text = @"收  货  人：";
        [self addSubview:self.nameKeyLabel];
        
        //收货人 value
        self.nameValueLabel = [[UITextField alloc] initWithFrame:CGRectOf([self.nameKeyLabel.ufo fx:UB_RIGHT fy:UB_TOP],
                                                                      [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin ow:-kOrderCellKeyLabelWidth oh:kOrderCellLabelHeight])];
        self.nameValueLabel.textAlignment = NSTextAlignmentLeft;
        self.nameValueLabel.textColor = HZ_BLACK_COLOR;
        self.nameValueLabel.font = kHZFont_Content_S;
        self.nameValueLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.nameValueLabel.userInteractionEnabled = YES;
        self.nameValueLabel.delegate = self;
        self.nameValueLabel.tag = 2000;
        self.nameValueLabel.returnKeyType = UIReturnKeyDone;
        [self addSubview:self.nameValueLabel];
        
        UIView *nameLine = [[UIView alloc] initWithFrame:CGRectOf([self.nameValueLabel.ufo fx:UB_LEFT fy:UB_BOTTOM py:-4], [self.nameValueLabel.ubo fw:UB_WIDTH ph:1])];
        nameLine.backgroundColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR;
        [self addSubview:nameLine];
        
        //收货地址 key
        self.addressKeyLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.nameKeyLabel.ufo fx:UB_LEFT fy:UB_BOTTOM oy:kSizeMallViewMargin/4], [self.ubo ow:kOrderCellKeyLabelWidth oh:kOrderCellLabelHeight])];
        self.addressKeyLabel.textAlignment = NSTextAlignmentLeft;
        self.addressKeyLabel.textColor = HZ_MALL_TEXT_GRAY_COLOR;
        self.addressKeyLabel.font = kHZFont_Content_S;
        self.addressKeyLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.addressKeyLabel.text = @"收货地址：";
        [self addSubview:self.addressKeyLabel];
        
        //收货地址  value
        self.addressValueLabel = [[UITextField alloc] initWithFrame:CGRectOf([self.addressKeyLabel.ufo fx:UB_RIGHT fy:UB_TOP],
                                                                      [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin ow:-kOrderCellKeyLabelWidth oh:kOrderCellLabelHeight])];
        self.addressValueLabel.textAlignment = NSTextAlignmentLeft;
        self.addressValueLabel.textColor = HZ_BLACK_COLOR;
        self.addressValueLabel.font = kHZFont_Content_S;
        self.addressValueLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.addressValueLabel.userInteractionEnabled = NO;
        [self addSubview:self.addressValueLabel];
        
        UIView *addressLine = [[UIView alloc] initWithFrame:CGRectOf([self.addressValueLabel.ufo fx:UB_LEFT fy:UB_BOTTOM py:-4], [self.nameValueLabel.ubo fw:UB_WIDTH ph:1])];
        addressLine.backgroundColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR;
        [self addSubview:addressLine];
        
        CGFloat eventLabelWidth = [self.ubo fw:UB_WIDTH uw:-kSizeMallViewMargin*6]/3;
        
        //团组
        self.groupLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.addressKeyLabel.ufo fx:UB_LEFT fy:UB_BOTTOM oy:kSizeMallViewMargin/4],
                                                                         [self.addressKeyLabel.ubo ow:eventLabelWidth fh:UB_HEIGHT])];
        self.groupLabel.textAlignment = NSTextAlignmentCenter;
        self.groupLabel.textColor = HZ_BLACK_COLOR;
        self.groupLabel.font = kHZFont_Content_S;
        self.groupLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.groupLabel.userInteractionEnabled = YES;
        self.groupLabel.text = @"选择团组";
        self.groupLabel.layer.masksToBounds = YES;
        self.groupLabel.layer.borderWidth = [self.ubo pw:1];
        self.groupLabel.layer.borderColor = HZ_MALL_M_SEPARATOR_CELL_LINE.CGColor;
        self.groupLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *groupTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(groupLabelOnClick)];
        [self.groupLabel addGestureRecognizer:groupTap];
        [self addSubview:self.groupLabel];
        
        //选择楼牌号
        self.buildingLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ubo fx:UB_CENTER ox:-eventLabelWidth/2 oy:self.groupLabel.ufo.y],
                                                                  [self.addressKeyLabel.ubo ow:eventLabelWidth fh:UB_HEIGHT])];
        self.buildingLabel.textAlignment = NSTextAlignmentCenter;
        self.buildingLabel.textColor = HZ_BLACK_COLOR;
        self.buildingLabel.font = kHZFont_Content_S;
        self.buildingLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.buildingLabel.userInteractionEnabled = YES;
        self.buildingLabel.text = @"选择楼牌号";
        self.buildingLabel.layer.masksToBounds = YES;
        self.buildingLabel.layer.borderWidth = [self.ubo pw:1];
        self.buildingLabel.layer.borderColor = HZ_MALL_M_SEPARATOR_CELL_LINE.CGColor;
        self.buildingLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *buildTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buildLabelOnClick)];
        [self.buildingLabel addGestureRecognizer:buildTap];
        [self addSubview:self.buildingLabel];
        
        //选择房屋
        self.roomLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ubo fx:UB_RIGHT ux:-kSizeMallViewMargin ox:-eventLabelWidth oy:self.groupLabel.ufo.y],
                                                                  [self.addressKeyLabel.ubo ow:eventLabelWidth fh:UB_HEIGHT])];
        self.roomLabel.textAlignment = NSTextAlignmentCenter;
        self.roomLabel.textColor = HZ_BLACK_COLOR;
        self.roomLabel.font = kHZFont_Content_S;
        self.roomLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.roomLabel.userInteractionEnabled = YES;
        self.roomLabel.text = @"选择房屋";
        self.roomLabel.layer.masksToBounds = YES;
        self.roomLabel.layer.borderWidth = [self.ubo pw:1];
        self.roomLabel.layer.borderColor = HZ_MALL_M_SEPARATOR_CELL_LINE.CGColor;
        self.roomLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *roomTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(roomLabelOnClick)];
        [self.roomLabel addGestureRecognizer:roomTap];
        [self addSubview:self.roomLabel];
        
        
        //收货地址 key
        self.phoneKeyLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.addressKeyLabel.ufo fx:UB_LEFT fy:UB_BOTTOM oy:kSizeMallViewMargin/2+kOrderCellLabelHeight], [self.ubo ow:kOrderCellKeyLabelWidth oh:kOrderCellLabelHeight])];
        self.phoneKeyLabel.textAlignment = NSTextAlignmentLeft;
        self.phoneKeyLabel.textColor = HZ_MALL_TEXT_GRAY_COLOR;
        self.phoneKeyLabel.font = kHZFont_Content_S;
        self.phoneKeyLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.phoneKeyLabel.text = @"电       话：";
        [self addSubview:self.phoneKeyLabel];
        
        //收货地址  value
        self.phoneValueLabel = [[UITextField alloc] initWithFrame:CGRectOf([self.phoneKeyLabel.ufo fx:UB_RIGHT fy:UB_TOP],
                                                                         [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin ow:-kOrderCellKeyLabelWidth oh:kOrderCellLabelHeight])];
        self.phoneValueLabel.textAlignment = NSTextAlignmentLeft;
        self.phoneValueLabel.textColor = HZ_BLACK_COLOR;
        self.phoneValueLabel.font = kHZFont_Content_S;
        self.phoneValueLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.phoneValueLabel.returnKeyType = UIReturnKeyDone;
        self.phoneValueLabel.delegate = self;
        self.phoneValueLabel.tag = 2001;
        [self addSubview:self.phoneValueLabel];

        UIView *phoneLine = [[UIView alloc] initWithFrame:CGRectOf([self.phoneValueLabel.ufo fx:UB_LEFT fy:UB_BOTTOM py:-4], [self.nameValueLabel.ubo fw:UB_WIDTH ph:1])];
        phoneLine.backgroundColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR;
        [self addSubview:phoneLine];
    }
    
    return self;
}

- (void)displayViewWithModel:(HZOrderByerAddressModel *)model{
    
    self.model = model;
    
    
    self.nameValueLabel.text = model.receiver;
    
    if (model.groupID && model.groupID.length > 0) {
        self.groupLabel.text = model.groupName;
    }else{
        self.groupLabel.text = @"选择团组";
    }
    
    if (model.buildingID && model.buildingID.length > 0) {
        self.buildingLabel.text = model.buildingName;
    }else{
        self.buildingLabel.text = @"选择楼牌号";
    }
    
    if (model.roomID && model.roomID.length > 0) {
        self.roomLabel.text = model.roomName;
    }else{
        self.roomLabel.text = @"选择房屋";
    }
    
    self.addressValueLabel.text = model.fullAddress;
    
    self.phoneValueLabel.text = model.phone;
    
    

    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.nameValueLabel resignFirstResponder];
    [self.phoneValueLabel resignFirstResponder];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.tag == 2000) {
        self.model.receiver = textField.text;
    }
    
    if (textField.tag == 2001) {
        self.model.phone = textField.text;
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.nameValueLabel resignFirstResponder];
    [self.phoneValueLabel resignFirstResponder];
    return YES;
}


//点击选择团组
- (void)groupLabelOnClick{
    if (self.targetCallerRoom) {
        [self.targetCallerRoom call:@"1"];
    }
}
//点击选择楼牌号
- (void)buildLabelOnClick{
    
    if (self.model.groupID && self.model.groupID.length > 0) {
        if (self.targetCallerRoom) {
            [self.targetCallerRoom call:@"2"];
        }
    }else{
        [MyToast showDefaultBottonWithText:@"请选择组团"];
    }
    
   
}
//选择房屋
- (void)roomLabelOnClick{
    
    if (self.model.buildingID && self.model.buildingID.length > 0) {
        if (self.targetCallerRoom) {
            [self.targetCallerRoom call:@"3"];
        }
    }else{
        [MyToast showDefaultBottonWithText:@"请选择楼牌号"];
    }
    
}
@end
